package com.ruoyi.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.finance.domain.DTO.KltPurchaseBatchDTO;
import com.ruoyi.finance.domain.Enum.PurchaceType;
import com.ruoyi.finance.domain.KltPurchaseDetail;
import com.ruoyi.finance.mapper.KltPurchaseBatchDao;
import com.ruoyi.finance.domain.KltPurchaseBatch;
import com.ruoyi.finance.mapper.KltPurchaseDetailDao;
import com.ruoyi.finance.service.KltPurchaseBatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * (KltPurchaseBatch)表服务实现类
 *
 * @author makejava
 * @since 2024-12-19 12:18:03
 */
@Service("kltPurchaseBatchService")
public class KltPurchaseBatchServiceImpl extends ServiceImpl<KltPurchaseBatchDao, KltPurchaseBatch> implements KltPurchaseBatchService {
    @Resource
    private KltPurchaseBatchDao kltPurchaseBatchDao;
    @Autowired
    private KltPurchaseDetailDao kltPurchaseDetailDao;

    @Override
    public List<KltPurchaseBatch> getAllKltPurchaseBatch(Wrapper<KltPurchaseBatch> kltPurchaseBatch) {
        List<KltPurchaseBatch> kltPurchaseBatches = kltPurchaseBatchDao.selectList(kltPurchaseBatch);
        return  kltPurchaseBatches;
    }

    @Override
    public Integer getMaxId() {
        QueryWrapper<KltPurchaseBatch> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("purchase_id").last("LIMIT 1");
        List<KltPurchaseBatch> PurchaseBatch = kltPurchaseBatchDao.selectList(queryWrapper);
        if (PurchaseBatch.isEmpty()) {
            return null; // 如果没有找到任何记录，则返回null或根据需要返回其他默认值
        }
        return PurchaseBatch.get(0).getPurchaseId();
    }


    @Override
    public List<KltPurchaseBatch> selectBySales(Integer salesOrderId) {
        List<KltPurchaseBatch> kltPurchaseBatches = kltPurchaseBatchDao.selectBySales(salesOrderId);


        for (KltPurchaseBatch kltPurchaseBatch : kltPurchaseBatches) {
            checkUpdateBatchStatus(kltPurchaseBatch);
            if(kltPurchaseBatch.getPurchaseType().equals("0")){
                kltPurchaseBatch.setPurchaseType((PurchaceType.Purchased.getInfo()));
            }
            else if(kltPurchaseBatch.getPurchaseType().equals("1")){
                kltPurchaseBatch.setPurchaseType((PurchaceType.notReceived.getInfo()));
            }
            else if(kltPurchaseBatch.getPurchaseType().equals("2")){
                kltPurchaseBatch.setPurchaseType(PurchaceType.notPaid.getInfo());
            }
            else if(kltPurchaseBatch.getPurchaseType().equals("3")){
                kltPurchaseBatch.setPurchaseType((PurchaceType.Ended.getInfo()));
            }
        }
        return kltPurchaseBatches;
    }


    @Override
    public Boolean InsertPurchaseBatchSalesOrder(Integer purchaseId, Integer salesOrderId) {
        Integer b = kltPurchaseBatchDao.InsertPurchaseBatchSalesOrder(purchaseId, salesOrderId);
        return b>0;
    }




    //   检查更新批次到货状态
    private void checkUpdateBatchStatus(KltPurchaseBatch kltPurchaseBatch) {
        List<KltPurchaseDetail> KltPurchaseDetailList= kltPurchaseDetailDao.selectList(new QueryWrapper<KltPurchaseDetail>()
                .eq("purchase_id", kltPurchaseBatch.getPurchaseId())
                .eq("is_del",0));

        int j = 0;
        for (KltPurchaseDetail kltPurchaseDetail : KltPurchaseDetailList) {
            if(!Objects.equals(kltPurchaseDetail.getIsArrived(), "2")){
                j++;
            }
        }
        if (j==0){
            if(Objects.equals(kltPurchaseBatch.getPurchaseType(), "1")){
//                如果已付款完毕就改为已完成
                kltPurchaseBatch.setPurchaseType("3");
                int i = kltPurchaseBatchDao.updateById(kltPurchaseBatch);
                if(i==0){
                    throw new RuntimeException("更新批次状态失败");
                }
            }
            else if(Objects.equals(kltPurchaseBatch.getPurchaseType(), "0")){
//                如果未付款完毕就改为已到货未付款

                kltPurchaseBatch.setPurchaseType("2");
                int i = kltPurchaseBatchDao.updateById(kltPurchaseBatch);
                if(i==0){
                    throw new RuntimeException("更新批次状态失败");
                }

            }
        }else{
            if(Objects.equals(kltPurchaseBatch.getPurchaseType(), "2")){
//                如果已付款完毕就改为已完成
                kltPurchaseBatch.setPurchaseType("0");
                int i = kltPurchaseBatchDao.updateById(kltPurchaseBatch);
                if(i==0){
                    throw new RuntimeException("更新批次状态失败");

                }
            }
            else if(Objects.equals(kltPurchaseBatch.getPurchaseType(), "3")){
//                如果未付款完毕就改为已到货未付款

                kltPurchaseBatch.setPurchaseType("1");
                int i = kltPurchaseBatchDao.updateById(kltPurchaseBatch);
                if(i==0){
                    throw new RuntimeException("更新批次状态失败");
                }

            }
        }
    }
}

